package com.codingfuture.glacioralarm.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;

import java.util.ArrayList;


@Configuration
@ComponentScan("com.codingfuture.glacioralarm")
public class RedisConfig {

    /**
     * 监听器容器 负责：管理线程池 消息分发，分发给对应管道的监听者
     * @param messageListener
     * @return
     */
    @Bean
    public RedisMessageListenerContainer container(MessageListener messageListener,
                                                   RedisConnectionFactory redisConnectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        //设置连接工厂
        container.setConnectionFactory(redisConnectionFactory);
         //设置监听者绑定管道
        ArrayList<Topic> topList = new ArrayList<>();
        topList.add(new PatternTopic("htDate"));//htDate管道
        //第一个参数：监听者，第二个参数：绑定管道
        container.addMessageListener(messageListener, topList);
        return container;
    }
}